package com.hf.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hf.entity.Students;
import com.hf.entity.StudentsParam;
import com.hf.entity.Users;
import com.hf.entity.UsersParam;
import com.hf.mapper.StudentsMapper;
import com.hf.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hf
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/students")
public class StudentsController {
@Autowired
StudentsMapper mapper;

/**
 * 多条件分页查询 localhost:8080/users/getList?page=0&limit=10
 * 
 * @param u
 * @return
 */
@RequestMapping("/getList")
public Object getList(StudentsParam u) {
	
	List<Students> list=mapper.getList(u);
	u.setPage(null);
	u.setLimit(null);
	List<Students> list2=mapper.getList(u);
	return new Result(0, "查詢成功",list,list2.size() );
	
	

}
@RequestMapping("/getMyList")
public Object getMyList(StudentsParam u) {

	List<Students> list=mapper.getList(u);
	u.setPage(null);
	u.setLimit(null);
	List<Students> list2=mapper.getList(u);
	return new Result(0, "查詢成功",list,list2.size() );
	
	
	
	
	/*
	  QueryWrapper<Students> q = new QueryWrapper<>(); if( u.getStuName()!=null &&
	  u.getStuName()!="") { q.like("stu_name",u.getStuName()); }
	  if(u.getStuSex()!=null && u.getStuSex()!=' ') {
	  q.eq("stu_sex",u.getStuSex()); } if(u.getStuState()!=null &&
	  u.getStuState()!="") { q.eq("stu_state", u.getStuState()); }
	  if(u.getStuUser()!=null && u.getStuUser()!=' ') { q.eq("stu_user",
	  u.getStuUser()); } if(u.getPage()!=null && u.getLimit()!=' ') {
	  q.eq("stu_user", u.getStuUser()); } System.out.println(u.getPage());
	  Page<Students> p = new Page<>(u.getPage(),u.getLimit()); IPage<Students>
	  list=mapper.selectPage(p,q); int a=(int)list.getTotal(); return new Result(0,
	  "查詢成功",list, a);
	 */

}
/*
 * @RequestMapping("/getMyList") public Object getMyList(StudentsParam u) {
 * 
 * List<Students> list=mapper.getMyList(u);
 * System.out.println(u.getLoginName()); return new Result(0,
 * "查詢成功",list,list.size() );
 * 
 * }
 */

@RequestMapping("/getWinList")
public Object getWinList(StudentsParam u) {
	
	List<Students> list=mapper.getWinList(u);
	u.setPage(null);
	u.setLimit(null);
	List<Students> list2=mapper.getWinList(u);
	return new Result(0, "查詢成功",list,list2.size() );

}

	/**
	 * 添加 localhost:8080/Students/insert?loginName=赵云&password=111111&isLockOut=0
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/insert")
	public Object insert(Students u) {
		if (mapper.insert(u)==1) {
			return new Result(0, "添加成功");
		}
		return new Result(1, "添加失败");
	}

	/**
	 * 删除 localhost:8080/Students/delete?id=1
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping("/delete")
	public Object delete(Integer id) {
			if (mapper.deleteById(id)==1) {
				return new Result(0, "删除成功");
			}
			return new Result(1, "删除失败");
		} 


	/**
	 * 修改 localhost:8080/Students/update?loginName=张飞&id=2
	 * 
	 * @param u
	 * @return
	 */
	@PostMapping("/updList")
	public Object update(Students u) {

		if (mapper.updateById(u)==1) {
			return new Result(0, "修改成功");
		}
		return new Result(1, "修改失败");
	}

	
	
	
}

